package com.example.adminframework.response;

import com.example.adminframework.constant.ErrorCode;
import com.example.adminframework.constant.HttpCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


/**
 * @Description 全局返回对象包裹
 */
@ApiModel("统一返回值")
public class ResultContent<T> {
    /**
     * 返回数据
     */
    @ApiModelProperty("返回数据")
    private T data;

    /**
     * HTTP_CODE
     */
    @ApiModelProperty("状态码")
    private Integer code;

    /**
     * 状态信息 建议生产环境统一异常信息 或 显示用户可认信息
     */
    @ApiModelProperty("状态信息")
    private String message;

    /**
     * 无返回数据成功响应
     *
     * @return {@link ResultContent}
     */
    public static ResultContent success() {
        return success(new EmptyResponse());
    }

    /**
     * 返回值带参数成功响应
     *
     * @return {@link ResultContent}
     */
    public static <T> ResultContent<T> success(T t) {
        return new ResultContent<>(HttpCode.OK, t);
    }

    /**
     * 有具体错误信息失败响应
     *
     * @param httpCode  httpCode
     * @param errorCode errorCode
     * @return {@link ResultContent}
     */
    public static ResultContent<ErrorCode> error(HttpCode httpCode, ErrorCode errorCode) {
        return new ResultContent<>(httpCode, errorCode);
    }


    /**
     * 有返回值自定义code响应
     *
     * @param httpCode httpCode
     * @return {@link ResultContent<T>}
     */
    public static <T> ResultContent<T> error(HttpCode httpCode, T t) {
        return new ResultContent<>(httpCode, t);
    }

    public ResultContent(HttpCode httpCode, T data) {
        this.data = data;
        this.code = httpCode.getCode();
        this.message = httpCode.getMessage();
    }

    public ResultContent() {
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    @Override
    public String toString() {
        return "ResultContent{" +
                "data=" + data +
                ", code=" + code +
                ", message='" + message + '\'' +
                '}';
    }

}
